package com.whh.controller;

import java.io.File;
import java.io.IOException;

import com.whh.config.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.whh.entity.ResultEntity;
import com.whh.entity.SysFileEntity;
import com.whh.service.SysFileService;

@RestController
@RequestMapping(value = "/file")
public class SysFileController extends BaseController<SysFileEntity> {
	
	@Autowired
	public SysFileController(SysFileService service) {
		super();
		setService(service);
	}
	
	@Override
	public ResultVO<SysFileEntity> add(SysFileEntity entity) {
		// TODO Auto-generated method stub
		return super.add(entity);
	}

    @PostMapping("/upload")
    @ResponseBody
    public ResultEntity upload(MultipartFile file,SysFileEntity entity) {
    	
    	ResultEntity resultEntity = new ResultEntity();
        if (file.isEmpty()) {
        	resultEntity.setMsg("上传失败，请选择文件");
            return resultEntity;
        }

        String fileName = file.getOriginalFilename();
        String filePath = "D://uploadFiles//";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            resultEntity.setSuccess(Boolean.TRUE);
        } catch (IOException e) {
        	resultEntity.setSuccess(Boolean.FALSE);
        }
        if(resultEntity.getSuccess()) {
        	entity.setFilePosition(dest.getAbsolutePath());
        	super.add(entity);
        }else {
			resultEntity.setMsg("上传失败");
		}
        return resultEntity;
    }
}
